package com.mon.test;

import java.lang.management.ManagementFactory;
import java.util.Properties;

import com.mon.utils.LoadUtils;
import com.sun.tools.attach.VirtualMachine;


public class PostJvmAgentLoader {
	
	// static final Logger logger = LoggerFactory.getLogger(PostJvmAgentLoader.class);
	
	 private static final String jarFilePath=LoadUtils.extractJarUrlLocation();
	
	
	    public static void loadAgent() {
	    	 System.out.println("File path : "+jarFilePath);
	     //   logger.info("dynamically loading javaagent");
	        String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
	        int p = nameOfRunningVM.indexOf('@');
	        String pid = nameOfRunningVM.substring(0, p);
	        System.out.println("Testinhg");
	        try {
	            VirtualMachine vm = VirtualMachine.attach(pid);
	            Properties props = vm.getSystemProperties();
	            // construct path to management agent
	            vm.loadAgent(jarFilePath, "");
	            vm.detach();
	        } catch (Exception e) {
	            throw new RuntimeException(e);
	        }
	        System.out.println("Test Complete");
	    }

	 

}
